Import Excel File

  • 1. STEP

    1. Package required

    
                        composer require maatwebsite/excel
                        

    2. Configure the Package

    config/app.php

    
                        'providers' => [
                            // ...
                            Maatwebsite\Excel\ExcelServiceProvider::class,
                        ],
    
                        'aliases' => [
                            // ...
                            'Excel' => Maatwebsite\Excel\Facades\Excel::class,
                        ],
    
                        

    Next, publish the package configuration file by running the following command:

    
                            php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
                             

    3. Define import class

    app/Imports/BookingImport.php

    
                        namespace App\Imports;
                        
                        use Maatwebsite\Excel\Concerns\ToModel;
                        
                        class BookingImport implements ToModel
                        {
                            public function model(array $row)
                            {
                                // Define how to create a model from the Excel row data
                                return new Booking([
                                    'column1' => $row[0],
                                    'column2' => $row[1],
                                    // Add more columns as needed
                                ]);
                            }
                        }
    
                        

    4. in controller

    
                          
                          use Maatwebsite\Excel\Facades\Excel;
                          use App\Imports\BookingImport;
                          use App\Models\Booking;
    
                          public function import(Request $request)
                            {
                                // Validate the uploaded file
                                $request->validate([
                                    'file' => 'required|mimes:xlsx,xls',
                                ]);
                        
                                // Get the uploaded file
                                $file = $request->file('file');
                        
                                // Process the Excel file
                                Excel::import(new BookingImport, $file);
                        
                                return redirect()->back()->with('success', 'Excel file imported successfully!');
                            }
    
    
                        

    5. Live example

    
    
                        namespace App\Imports;
                        use App\Models\User;
                        use Illuminate\Support\Facades\Hash;
                        use Maatwebsite\Excel\Concerns\ToModel;
                        use Maatwebsite\Excel\Concerns\WithHeadingRow;
                        use App\Models\Parts;
    
    
    
                        class PartsImport implements ToModel
                        {
                            /**
                            * @param  array $row
                            *
                            * @return  \Illuminate\Database\Eloquent\Model|null
                            */
                            public function model(array $row)
                            {
                                // return new User([
                                //     'name'     => $row[0],
                                //     'email'    => $row[1],
                                //     'password' => Hash::make($row[2])
                                // ]);
    
                                $parts=Parts::where('number',$row[0])->first();
                                if(isset($parts)){
                                    $parts->description=$row[1];
                                    $parts->manufacturers_id=$row[2];
                                    $parts->price=$row[3];
                                    $parts->save();
                                }
                                else{
                                    Parts::create([
                                        'number'=>$row[0],
                                        'description'=>$row[1],
                                        'manufacturers_id'=>$row[2],
                                        'price'=>$row[3]
                                    ]);
                                }
    
                                print_r($row);
                            }
                        }